package com.polaris.live.ui.main.game

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.polaris.live.adapter.home.FriendShipAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.databinding.FragmentHomeGameBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.GameVo
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.ui.game.GameUtils
import com.polaris.live.ui.main.live.AbstractLiveListFragment
import com.polaris.live.utils.LiveUtils
import com.polaris.live.viewmodel.home.HomeGameViewModel

/**
 * HomeGameFragment 首页游戏
 * @author Created by 天晴 on 2024/1/18 19:08
 * @since 1.0.0
 **/
class HomeGameFragment : AbstractLiveListFragment<HomeGameViewModel, FragmentHomeGameBinding>() {

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy { LinearLayoutManager(context) }

    override val mAdapter by lazy { FriendShipAdapter(FriendShipAdapter.GAME, viewLifecycleOwner.lifecycleScope) }

    override val listLiveData by lazy { mViewModel.mListLiveData }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        setRefreshTime(FIVE_MINUTES)
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            DebounceHelper.singleClick {
                val item = mAdapter.getItemOrNull(position) as? DisplayUserBean
                if (item != null) {
                    onItemClick(item)
                }

                val itemGame = mAdapter.getItemOrNull(position) as? GameVo
                if (itemGame != null) {
                    GameUtils.showGameActivity(requireContext(), itemGame)
                }
            }
        }

        mViewModel.mListLiveData.observe(this) { resultState ->
            parseState(resultState, {
                val isHava = it.list.any { it is DisplayUserBean }
                val isHaveGame = it.list.any { it is GameVo }
                if (isHava || isHaveGame) {
                    mBinding.refresh.showEmptyView(View.GONE)
                } else {
                    val isHaveBanner = it.list.any { it is ListWithBannerBean }
                    if (isHaveBanner) {
                        mBinding.refresh.showEmptyView(View.VISIBLE)
                    } else {
                        mBinding.refresh.showEmptyView(View.GONE)
                    }
                }
            })
        }
        mBinding.ivStartParty.setOnSingleClickListener {
            LiveUtils.openPartyChecking(requireContext(), mViewModel)
        }
    }

    private fun onItemClick(item: DisplayUserBean) {

        if (item.base.userId == UserManager.getUserId()) {
            mViewModel.getPartyRoom()
        } else {
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                LiveUtils.invokeSpectator(requireContext(), item)
            })
        }
    }
}